'use strict';

	var app = angular.module('app');
    app.controller('updateSecurityController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster','$modal',
        function($scope, $rootScope, $state, $timeout, toaster, $modal) {
            if($state.includes('**.security.update')){
            	$scope.loading = true;
            	$scope.reset = false;//修改不允许重置
                $scope.title="修改权限";
                var id = $state.params.id;
                activate(id);
            }else if($state.includes('**.security.create')){
                $scope.title="新增权限";
                $scope.reset = true;//新增时可以重置
            }
            validate();
            $scope.resetFrom = function(){
   				$scope.record ={};
			}
            $scope.selUser = function(){
            	$scope.record.personId=$scope.record.person.split('_')[0];
            	$scope.record.cardNo=$scope.record.person.split('_')[1];
            }
            $scope.submit= function(){
                var m = $scope.record;
                if(m){
                	$scope.record.status = '0';
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
        				type: 'POST',
        	            dataType: 'json',
        				contentType:'application/json;charset=UTF-8',
        				url : '/userManager/saveOrUpdate',
        				data: angular.toJson($scope.record)
        			}).then(function(result){
                        if(result.httpCode ==200){//成功
                            toaster.clear('*');
                            toaster.pop('success', '', "保存成功");
                            $timeout(function(){
                                $state.go('main.biz.security.list');
                            },2000);
                        }else{
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                            $scope.isDisabled = false;
                        }
                    });
                }
                
            }

            var handleFileSelect=function(evt) {
                var file=evt.currentTarget.files[0];
                if(!/image\/\w+/.test(file.type)){
                    return false;
                }
                var reader = new FileReader();
                reader.onload = function (evt) {
                    $scope.$apply(function($scope){
                        $scope.myImage=evt.target.result;
                    });
                };
                reader.readAsDataURL(file);
            };
            angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);


            // 初始化页面
            function activate(id) {
    	        $scope.loading = true;
            	$.ajax({
            		type: 'PUT',
    	            dataType: 'json',
    				contentType:'application/json;charset=UTF-8',
    				url : '/userManager/detail',
    				data: angular.toJson({'id': id})
    			}).then(function(result) {
    		        $scope.loading = false;
    				if (result.httpCode == 200) {
    					$scope.record = result.data;
    				} else {
    					$scope.msg = result.msg;
    				}
    				$scope.$apply();
    			});
            }

            //表单验证
            function validate(userId){
                jQuery('form').validate({
                    submitHandler: function() {
                        $scope.submit();
                    }
                });
            }

        }]);